#!/bin/bash

cd ~/share/eth2.0/ethereum/execution

# =============== ps ==================
target=./geth

flag=true
pids=`ps aux | grep ${target} | grep -v grep | grep -v $0 | awk '{print $2}'`
if [ -n "$pids" ]
then
  for pid in ${pids}
  do
    read -p "Do you want to kill `ps $pid` ? (n/y)" isKill
    if [ "$isKill" == "y" ]; then
      echo "Yes"
      kill -9 $pid
      flag=true
    else
      flag=false
      echo "No"
    fi
  done
fi

if [ "$flag" == true ]; then
  echo "Restarting..."
else
  echo "Exit"
  exit -1
fi
# =============== ps ==================

# =============== log ==================
logname=geth
logfolder=~/workspace/demo/project/blockchain/eth2/devlog

historylogfile="$logfolder/$logname"_`date +%Y-%m`.log
logfile=$logfolder/$logname.log

if [ ! -d "$logfolder" ]; then
  mkdir -p $logfolder
fi

if [ -e "$logfile" ]; then
  echo "" >> $historylogfile
  echo "=========================================================" >> $historylogfile
  echo "" >> $historylogfile
  cat $logfile >> $historylogfile
fi

echo "" > $logfile
# =============== log ==================

nohup ./geth \
  --datadir gethdata \
  --http.addr 0.0.0.0 \
  --http --http.api eth,net,engine,admin,debug,txpool,personal,web3 \
  --ws.addr 0.0.0.0 \
  --ws --ws.api personal,eth,net,web3,txpool \
  --syncmode=full \
  --authrpc.jwtsecret ../consensus/jwt.hex \
  --allow-insecure-unlock \
  --unlock="0x123463a4b065722e99115d6c222f267d9cabb524" \
  --password password \
  --nodiscover \
  --mine &>> $logfile &
